import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class MaterialItem {
  MaterialItem({
    this.id,
    this.cover,
    this.code,
  });

  factory MaterialItem.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : MaterialItem(
          id: asT<int>(jsonRes['id']),
          cover: asT<String>(jsonRes['cover']),
          code: asT<String>(jsonRes['code']),
        );

  int id;
  String cover;
  String code;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'cover': cover,
        'code': code,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
